package com.raja.servingStation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.constant.Constants;
import com.raja.servingStation.common.constant.MysqlPageVo;
import com.raja.servingStation.dao.HistoryOrderDao;
import com.raja.servingStation.entity.Engineer;
import com.raja.servingStation.entity.RepairOrderResp;
import com.raja.servingStation.service.HistoryOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangzhen on 2017/10/10.
 */
@Service
public class HistoryOrderServiceImpl implements HistoryOrderService {
    @Autowired
    private HistoryOrderDao historyOrderDao;

    @Override
    public MysqlPageVo queryOrderListByPage(JSONObject body,Integer stationId) throws Exception {
        Integer pageSize = body.getInteger("pageSize");
        Integer currPage = body.getInteger("currPage");
        MysqlPageVo<Engineer> mysqlPageVo = new MysqlPageVo<Engineer>();
        if (pageSize == null){
            pageSize = Constants.pageSize;
        }
        if (currPage == null){
            currPage = Constants.currPage;
        }
        mysqlPageVo.setPageSize(pageSize, currPage);


        List<RepairOrderResp> list = new ArrayList<>();

        int count1 = historyOrderDao.getRepairOrderCount(body,stationId).intValue();
        if(count1 >0) {
            List<RepairOrderResp> repairOrderList = historyOrderDao.getRepairOrderList(body,mysqlPageVo,stationId);
            for (RepairOrderResp aRepairOrderList : repairOrderList) {
                aRepairOrderList.setOrderType(1);//维修
            }
            list.addAll(repairOrderList);
        }
        int count2 = historyOrderDao.getFirstInsuranceOrderCount(body,stationId).intValue();
        if(count2 >0){
            List<RepairOrderResp> firstInsuranceOrderList = historyOrderDao.getFirstInsuranceOrderList(body,mysqlPageVo,stationId);
            for (RepairOrderResp aFirstInsuranceOrderList : firstInsuranceOrderList) {
                aFirstInsuranceOrderList.setOrderType(2);//首保
            }
            list.addAll(firstInsuranceOrderList);
        }
        int count = count1 + count2;
        mysqlPageVo.setTotal(count);
        mysqlPageVo.setData(list);
        return mysqlPageVo;
    }

}
